package ui.swing.table;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;

/**
 * 功能：增加、删除行列的简单例子
 *       删除列数据
 * @author 林可
 * @version 1.0 2009-11-10
 */
public class Table_RowColumn_CD extends JFrame implements ActionListener{

	private static final long serialVersionUID = 1499519533734560205L;
	
	int cIndex = 1;
	JTable table = null;
	DefaultTableModel dtm = null;

	public Table_RowColumn_CD() {
		super("增加、删除行列的简单例子");
		
		setBounds(200, 200, 600, 400);
		
		String[] name = {"字段1","字段2","字段3","字段4","字段5"};
		String[][] data = new String[][]{
				{"11","12","13","14","15"},
				{"21","22","23","24","25"},
				{"31","32","33","34","35"},
				{"41","42","43","44","45"},
				{"51","52","53","54","55"},
		};
		
		dtm = new DefaultTableModel(data, name);
       	table = new JTable(dtm);

		// 使得表格的选取是以cell为单位,而不是以列为单位.若你没有写此行,则在选取表格数
        // 据时以整列为单位.
		table.setCellSelectionEnabled(true);
		// table.setSelectionModel(new MyListSelectionModel());

		JPanel pnl_tblContainer = new JPanel(new BorderLayout());
		pnl_tblContainer.add(table, BorderLayout.CENTER);
		pnl_tblContainer.add(table.getTableHeader(), BorderLayout.NORTH);
		
		JPanel pnl_btnContainer = new JPanel();
		JButton b=new JButton("增加行");
		b.addActionListener(this);
		pnl_btnContainer.add(b);

		b = new JButton("删除行");
		b.addActionListener(this);
		pnl_btnContainer.add(b);

		b = new JButton("增加列");
		b.addActionListener(this);
		pnl_btnContainer.add(b);

		b = new JButton("删除列");
		b.addActionListener(this);
		pnl_btnContainer.add(b);

		getContentPane().add(pnl_btnContainer, BorderLayout.NORTH);
		getContentPane().add(pnl_tblContainer, BorderLayout.CENTER);
		
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setVisible(true);
	}

	public void actionPerformed(ActionEvent e) {
		if("增加行".equals(e.getActionCommand())) {
			dtm.addRow(new Vector());
		}
		if("删除行".equals(e.getActionCommand())) {
			int rowCount = dtm.getRowCount();
			if( rowCount > 0 )
			{
				dtm.removeRow(--rowCount);
				// 行完毕后必须重新设置列数，也就是使用DefaultTableModel
				// 的setRowCount()方法来设置。
				dtm.setRowCount(rowCount);
			}
		}
		if("增加列".equals(e.getActionCommand())) {
			// 通过DefaultTableModel添加
			System.out.println(table.getColumnCount());
			dtm.addColumn("'nc' " + cIndex++);
			System.out.println(table.getColumnCount());
			
			// 通过TableColumnModel添加
			/*System.out.println(table.getColumnCount());
			TableColumn tc = new TableColumn();
			tc.setHeaderValue("hi baby!");
			table.getColumnModel().addColumn(tc);
			System.out.println(table.getColumnCount());*/
		}
		if("删除列".equals(e.getActionCommand())) {
			int columnCount = dtm.getColumnCount();
			if( columnCount > 0 )
			{
				TableColumnModel tcm = table.getColumnModel();
				tcm.removeColumn(tcm.getColumn(--columnCount));
				dtm.setColumnCount(columnCount);
			}
		}
		table.revalidate();
	}
	
	public static void main(String[] args) {
		new Table_RowColumn_CD();
	}
}
